package cn.edu.suda.ada.database.objects;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 数据表类
 * 
 * @author JiangJun
 */
public class Table {
	/**
	 * 数据表表名
	 */
	private String name;
	/**
	 * 表中的列集合
	 */
	private Set<Column<?>> columns = new LinkedHashSet<Column<?>>();
	/**
	 * 表中索引集合
	 */
	private Set<Key> keys = new HashSet<Key>();
	/**
	 * 数据表引擎类型
	 */
	private Engine engine = Engine.INNODB;
	/**
	 * 数据表字符集
	 */
	private Charset charset = Charset.UTF8;
	/**
	 * 注释
	 */
	private String comment = "";

	/**
	 * 数据库中的字符集枚举类型
	 */
	public enum Charset {
		UTF8("utf8"), GBK("GBK"),GB2312("GB2312");
		private String charset;
		
		Charset(String charset) {
			this.charset = charset;
		}
		
		@Override
		public String toString() {
			return charset;
		}
	}

	/**
	 * 数据表类型枚举类型
	 */
	public enum Engine {
		INNODB("InnoDB"), MYISAM("MyISAM");
		private String engine;

		Engine(String engine) {
			this.engine = engine;
		}

		@Override
		public String toString() {
			return engine;
		}

		/**
		 * 根据字符串获取数据表类型
		 * 
		 * @param engine 数据表类型字符串
		 * @return 数据库引擎
		 */
		public static Engine getEngine(String engine) {
			if (engine != null) return Enum.valueOf(Engine.class, engine.trim().toUpperCase());
			return null;
		}
	}

	public Table() {
		super();
	}
	
	public Table(String tableName) {
		super();
		this.name = tableName;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Engine getEngine() {
		return engine;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

	public Charset getCharset() {
		return charset;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Set<Column<?>> getColumns() {
		return columns;
	}

	public void setColumns(Set<Column<?>> columns) {
		this.columns = columns;
	}

	public Set<Key> getKeys() {
		return keys;
	}

	public void setKeys(Set<Key> keys) {
		this.keys = keys;
	}
	
	/**
	 * 新增一列
	 * 
	 * @param col 数据列实例
	 */
	public void addColumn(Column<?> col) {
		this.columns.add(col);
	}

	/**
	 * 删除一列
	 * 
	 * @param col 数据列实例
	 */
	public void removeColumn(Column<?> col) {
		this.columns.remove(col);
	}
	
	/**
	 * 获得具体的一列
	 * 
	 * @param columnName 列名
	 * @return 数据列实例
	 */
	public Column<?> getColumn(String columnName){
		if (!columns.isEmpty()) {
			for (Column<?> column : columns){
				if (column.getName().equals(columnName)){
					return column;
				}
			}
		}
		return null;
	}

	/**
	 * 新增索引
	 * 
	 * @param key Key 索引实例
	 */
	public void addKey(Key key) {
		this.keys.add(key);
	}

	/**
	 * 删除索引
	 * 
	 * @param key Key 索引实例
	 */
	public void removeKey(Key key) {
		this.keys.remove(key);
	}

	/**
	 * 获得所有列名
	 * 
	 * @return Set 有序集合
	 */
	public Set<String> getColumnNames(){
		Set<String> columnNames = new LinkedHashSet<String>();
		for(Column<?> column : columns){
			columnNames.add(column.getName());
		}
		return columnNames;
	}

	@Override
	public String toString() {
		return String.format("Table : %s ( Engine = %s , CharSet = %s )\n-Columns :\n%s\n-Keys :\n%s",
				name, engine, charset, columns, keys);
	}
}
